/**
 * 值班信息管理服务层
 */

// 模拟值班记录数据
const mockDutyRecords = [
  {
    id: 'DUTY-2025-1101',
    dutyPerson: '陈晓宇',
    dutyDate: '2025-11-05',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3201',
    emergencyContact: '王建国 (138-2568-3202)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '处理3起突发公共卫生事件咨询',
    remark: '值班期间正常，无异常情况',
    status: '已完成',
    createTime: '2025-11-05 08:00',
    endTime: '2025-11-05 20:00'
  },
  {
    id: 'DUTY-2025-1102',
    dutyPerson: '林雨薇',
    dutyDate: '2025-11-05',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3203',
    emergencyContact: '赵敏慧 (138-2568-3204)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '无',
    remark: '值班正常',
    status: '值班中',
    createTime: '2025-11-05 20:00',
    endTime: '2025-11-06 08:00'
  },
  {
    id: 'DUTY-2025-1103',
    dutyPerson: '孙凯旋',
    dutyDate: '2025-11-06',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3205',
    emergencyContact: '周若琳 (138-2568-3206)',
    duties: '监测疫情动态、数据统计分析、应急处置协调',
    events: '',
    remark: '',
    status: '待值班',
    createTime: '2025-11-06 08:00',
    endTime: '2025-11-06 20:00'
  },
  {
    id: 'DUTY-2025-1098',
    dutyPerson: '黄诗涵',
    dutyDate: '2025-11-04',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3207',
    emergencyContact: '宋文轩 (138-2568-3208)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '处理1起食品安全事件报告，协调相关部门调查处置',
    remark: '食品安全事件已妥善处理',
    status: '已完成',
    createTime: '2025-11-04 08:00',
    endTime: '2025-11-04 20:00'
  },
  {
    id: 'DUTY-2025-1099',
    dutyPerson: '许梓豪',
    dutyDate: '2025-11-04',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3209',
    emergencyContact: '马雅琪 (138-2568-3210)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '接到2起发热病人报告，已记录并通知相关单位',
    remark: '夜间值班正常',
    status: '已完成',
    createTime: '2025-11-04 20:00',
    endTime: '2025-11-05 08:00'
  },
  {
    id: 'DUTY-2025-1095',
    dutyPerson: '郭浩然',
    dutyDate: '2025-11-03',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3211',
    emergencyContact: '田欣怡 (138-2568-3212)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '协调处理学校突发疫情事件1起',
    remark: '及时上报并协调处置',
    status: '已完成',
    createTime: '2025-11-03 08:00',
    endTime: '2025-11-03 20:00'
  },
  {
    id: 'DUTY-2025-1096',
    dutyPerson: '谢嘉怡',
    dutyDate: '2025-11-03',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3213',
    emergencyContact: '何俊杰 (138-2568-3214)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '无',
    remark: '值班正常',
    status: '已完成',
    createTime: '2025-11-03 20:00',
    endTime: '2025-11-04 08:00'
  },
  {
    id: 'DUTY-2025-1092',
    dutyPerson: '余思远',
    dutyDate: '2025-11-02',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3215',
    emergencyContact: '邓慧敏 (138-2568-3216)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '处理5起群众咨询，提供防控指导',
    remark: '咨询量较大，已妥善解答',
    status: '已完成',
    createTime: '2025-11-02 08:00',
    endTime: '2025-11-02 20:00'
  },
  {
    id: 'DUTY-2025-1093',
    dutyPerson: '范彦博',
    dutyDate: '2025-11-02',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3217',
    emergencyContact: '秦雅诗 (138-2568-3218)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '无',
    remark: '值班正常',
    status: '已完成',
    createTime: '2025-11-02 20:00',
    endTime: '2025-11-03 08:00'
  },
  {
    id: 'DUTY-2025-1089',
    dutyPerson: '邹婉婷',
    dutyDate: '2025-11-01',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3219',
    emergencyContact: '程锦程 (138-2568-3220)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '接报并处理养老院流感疫情1起',
    remark: '已通知相关部门介入调查',
    status: '已完成',
    createTime: '2025-11-01 08:00',
    endTime: '2025-11-01 20:00'
  },
  {
    id: 'DUTY-2025-1090',
    dutyPerson: '夏景辉',
    dutyDate: '2025-11-01',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3221',
    emergencyContact: '贺思琪 (138-2568-3222)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '接到1起疑似传染病报告，已及时上报',
    remark: '夜间处置及时',
    status: '已完成',
    createTime: '2025-11-01 20:00',
    endTime: '2025-11-02 08:00'
  },
  {
    id: 'DUTY-2025-1086',
    dutyPerson: '梁子轩',
    dutyDate: '2025-10-31',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3223',
    emergencyContact: '武雨桐 (138-2568-3224)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '处理餐厅食源性疾病暴发事件1起，紧急协调流调队伍',
    remark: '应急响应迅速，处置得当',
    status: '已完成',
    createTime: '2025-10-31 08:00',
    endTime: '2025-10-31 20:00'
  },
  {
    id: 'DUTY-2025-1087',
    dutyPerson: '彭欣悦',
    dutyDate: '2025-10-31',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3225',
    emergencyContact: '方涵煜 (138-2568-3226)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '持续跟踪食源性疾病事件处置进展',
    remark: '配合白班完成事件处置',
    status: '已完成',
    createTime: '2025-10-31 20:00',
    endTime: '2025-11-01 08:00'
  },
  {
    id: 'DUTY-2025-1083',
    dutyPerson: '龙筱萱',
    dutyDate: '2025-10-30',
    shift: '白班(08:00-20:00)',
    location: '应急指挥中心',
    department: '应急管理科',
    phone: '138-2568-3227',
    emergencyContact: '阮俊熙 (138-2568-3228)',
    duties: '负责应急事件接报、信息记录、上报工作；协调应急资源调度',
    events: '接报麻疹病例1例，及时组织接触者追踪',
    remark: '应急处置及时',
    status: '已完成',
    createTime: '2025-10-30 08:00',
    endTime: '2025-10-30 20:00'
  },
  {
    id: 'DUTY-2025-1084',
    dutyPerson: '白嘉欣',
    dutyDate: '2025-10-30',
    shift: '夜班(20:00-次日08:00)',
    location: '应急指挥中心',
    department: '疫情防控科',
    phone: '138-2568-3229',
    emergencyContact: '叶承泽 (138-2568-3230)',
    duties: '负责夜间应急值守、信息收集与处理、紧急情况上报',
    events: '无',
    remark: '值班正常',
    status: '已完成',
    createTime: '2025-10-30 20:00',
    endTime: '2025-10-31 08:00'
  }
];

// 模拟导出历史记录
const mockExportHistory = [
  {
    id: 1,
    fileName: '值班信息_2025年11月_第一周',
    format: 'Excel',
    exportTime: '2025-11-05 16:30',
    fileSize: '0.8 MB',
    recordCount: 14,
    status: '导出成功'
  },
  {
    id: 2,
    fileName: '值班信息_2025年10月',
    format: 'PDF',
    exportTime: '2025-11-01 10:00',
    fileSize: '2.3 MB',
    recordCount: 62,
    status: '导出成功'
  },
  {
    id: 3,
    fileName: '值班信息_2025年10月_应急管理科',
    format: 'Word',
    exportTime: '2025-10-28 14:20',
    fileSize: '1.2 MB',
    recordCount: 31,
    status: '导出成功'
  },
  {
    id: 4,
    fileName: '值班信息_2025年第三季度',
    format: 'Excel',
    exportTime: '2025-10-10 09:15',
    fileSize: '3.5 MB',
    recordCount: 184,
    status: '导出成功'
  }
];

/**
 * 获取值班记录列表
 */
export function getDutyRecords(params = {}) {
  return new Promise((resolve) => {
    setTimeout(() => {
      let results = [...mockDutyRecords];
      
      // 按状态筛选
      if (params.status) {
        results = results.filter(r => r.status === params.status);
      }
      
      // 按部门筛选
      if (params.department) {
        results = results.filter(r => r.department === params.department);
      }
      
      // 按班次筛选
      if (params.shift) {
        results = results.filter(r => r.shift.includes(params.shift));
      }
      
      // 按关键词搜索
      if (params.keyword) {
        const keyword = params.keyword.toLowerCase();
        results = results.filter(r => 
          r.dutyPerson.toLowerCase().includes(keyword) ||
          r.location.toLowerCase().includes(keyword) ||
          r.department.toLowerCase().includes(keyword) ||
          r.duties.toLowerCase().includes(keyword)
        );
      }
      
      // 按日期范围筛选
      if (params.startDate && params.endDate) {
        results = results.filter(r => {
          return r.dutyDate >= params.startDate && r.dutyDate <= params.endDate;
        });
      }
      
      resolve(results);
    }, 500);
  });
}

/**
 * 获取单个值班记录详情
 */
export function getDutyDetail(dutyId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const duty = mockDutyRecords.find(d => d.id === dutyId);
      if (duty) {
        resolve(duty);
      } else {
        reject(new Error('值班记录不存在'));
      }
    }, 400);
  });
}

/**
 * 导出值班信息
 */
export function exportDutyInfo(dutyId, params = {}) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const duty = mockDutyRecords.find(d => d.id === dutyId);
      const { format = 'Excel', fields = [], options = {} } = params;
      
      const formatExtMap = {
        'Excel': 'xlsx',
        'PDF': 'pdf',
        'Word': 'docx',
        'Text': 'txt'
      };
      
      const ext = formatExtMap[format] || 'xlsx';
      const fileName = `值班信息_${duty ? duty.dutyPerson : '未知'}_${duty ? duty.dutyDate : ''}.${ext}`;
      
      const record = {
        id: Date.now(),
        fileName: fileName,
        format: format,
        exportTime: new Date().toLocaleString('zh-CN'),
        fileSize: `${(Math.random() * 0.5 + 0.3).toFixed(1)} MB`,
        recordCount: 1,
        status: '导出成功'
      };
      
      mockExportHistory.unshift(record);
      
      resolve({ 
        success: true,
        downloadUrl: `/downloads/duty/${dutyId}.${ext}`,
        fileName: fileName,
        fileSize: record.fileSize
      });
    }, 1500);
  });
}

/**
 * 批量导出值班信息
 */
export function batchExportDutyInfo(dutyIds, params = {}) {
  return new Promise((resolve) => {
    setTimeout(() => {
      const { format = 'Excel' } = params;
      const count = dutyIds.length;
      
      const formatExtMap = {
        'Excel': 'xlsx',
        'PDF': 'pdf',
        'Word': 'docx',
        'Text': 'txt'
      };
      
      const ext = formatExtMap[format] || 'xlsx';
      const dateStr = new Date().toLocaleDateString('zh-CN').replace(/\//g, '-');
      let fileName;
      
      if (count > 1) {
        fileName = `值班信息批量导出_${dateStr}_${count}条记录.zip`;
      } else {
        fileName = `值班信息_${dateStr}.${ext}`;
      }
      
      const record = {
        id: Date.now(),
        fileName: fileName,
        format: format,
        exportTime: new Date().toLocaleString('zh-CN'),
        fileSize: `${(Math.random() * 2 + 0.5).toFixed(1)} MB`,
        recordCount: count,
        status: '导出成功'
      };
      
      mockExportHistory.unshift(record);
      
      resolve({ 
        success: true,
        downloadUrl: `/downloads/duty/batch_${Date.now()}.zip`,
        fileName: fileName,
        count: count
      });
    }, 2000);
  });
}

/**
 * 获取导出历史记录
 */
export function getExportHistory() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve([...mockExportHistory]);
    }, 400);
  });
}

/**
 * 获取值班统计数据
 */
export function getDutyStatistics() {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({
        totalRecords: mockDutyRecords.length,
        completedDuties: mockDutyRecords.filter(r => r.status === '已完成').length,
        onDuty: mockDutyRecords.filter(r => r.status === '值班中').length,
        pendingDuties: mockDutyRecords.filter(r => r.status === '待值班').length,
        byDepartment: {
          '应急管理科': mockDutyRecords.filter(r => r.department === '应急管理科').length,
          '疫情防控科': mockDutyRecords.filter(r => r.department === '疫情防控科').length
        },
        byShift: {
          '白班': mockDutyRecords.filter(r => r.shift.includes('白班')).length,
          '夜班': mockDutyRecords.filter(r => r.shift.includes('夜班')).length
        },
        totalEvents: mockDutyRecords.filter(r => r.events && r.events !== '无').length
      });
    }, 500);
  });
}

/**
 * 保存值班记录
 */
export function saveDutyRecord(dutyData) {
  return new Promise((resolve) => {
    setTimeout(() => {
      resolve({ 
        success: true,
        dutyId: dutyData.id || `DUTY-2025-${Math.floor(Math.random() * 10000)}`,
        message: '值班记录保存成功'
      });
    }, 800);
  });
}

/**
 * 删除值班记录
 */
export function deleteDutyRecord(dutyId) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      const index = mockDutyRecords.findIndex(d => d.id === dutyId);
      if (index !== -1) {
        mockDutyRecords.splice(index, 1);
        resolve({ success: true, message: '删除成功' });
      } else {
        reject(new Error('记录不存在'));
      }
    }, 600);
  });
}

/**
 * 获取值班人员列表
 */
export function getDutyPersonnel() {
  return new Promise((resolve) => {
    setTimeout(() => {
      const personnel = [
        { id: 1, name: '陈晓宇', department: '应急管理科', phone: '138-2568-3201' },
        { id: 2, name: '林雨薇', department: '应急管理科', phone: '138-2568-3203' },
        { id: 3, name: '孙凯旋', department: '疫情防控科', phone: '138-2568-3205' },
        { id: 4, name: '黄诗涵', department: '应急管理科', phone: '138-2568-3207' },
        { id: 5, name: '许梓豪', department: '疫情防控科', phone: '138-2568-3209' },
        { id: 6, name: '郭浩然', department: '应急管理科', phone: '138-2568-3211' },
        { id: 7, name: '谢嘉怡', department: '疫情防控科', phone: '138-2568-3213' },
        { id: 8, name: '余思远', department: '应急管理科', phone: '138-2568-3215' }
      ];
      resolve(personnel);
    }, 400);
  });
}

